//
//  YWHengVideoHeaderView.m
//  videoTest3
//
//  Created by jun peng on 2022/6/19.
//

#import "YWHengVideoHeaderView.h"

#import "YWHeaderVideoListCell.h"

#define YWVedioListID @"YWVedioListID"

#define YWVedioListInterval 4
#define YWVedioListeHeight 60
#define YWVedioListWidth YWVedioListeHeight*1.0/195*347

@interface YWHengVideoHeaderView ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIView *pastBgView;

@property(nonatomic,strong)YWGetNewsDataStatus *selectedListStatus;


@property (weak, nonatomic) IBOutlet UIButton *playBtn;
- (IBAction)playBtnClick:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UIImageView *iconView;


@end

@implementation YWHengVideoHeaderView

-(void)awakeFromNib{
    
    [super awakeFromNib];
    
    self.pastBgView.backgroundColor = [UIColor whiteColor];
    self.pastBgView.layer.shadowColor = [UIColor lightGrayColor].CGColor;//shadowColor阴影颜色
    self.pastBgView.layer.shadowOffset = CGSizeMake(2,0);//shadowOffset阴影偏移,x向右偏移4，y向下偏移4，默认(0, -3),这个跟shadowRadius配合使用
    self.pastBgView.layer.shadowOpacity = 0.2;//阴影透明度，默认0
    self.pastBgView.layer.shadowRadius = 3;//阴影半径，默认3
    
      //注册cell(xib创建用前者，代码创建用后者)
      [self.listCollectionView registerNib:[UINib nibWithNibName:@"YWHeaderVideoListCell" bundle:nil] forCellWithReuseIdentifier:YWVedioListID];
      //    [self.collectionView registerClass:[YWImageCell class] forCellWithReuseIdentifier:YWCellID];
    
    
      
}

+ (instancetype)headerView{
    
    return [[[NSBundle mainBundle] loadNibNamed:@"YWHengVideoHeaderView" owner:nil options:nil] firstObject];
    
}


-(void)setStatus:(YWGetNewsDataStatus *)status{
    
    _status = status;
    
    self.titleLabel.text = status.Title;
    
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:status.Img]];
    
    
    
    
}

- (IBAction)playBtnClick:(UIButton *)sender {
   
    if ([self.delegate respondsToSelector:@selector(hengVideoHeaderViewDidTap:withStatus:)]) {
        [self.delegate hengVideoHeaderViewDidTap:self withStatus:self.status];
    }
    
    
}


-(void)setStatusListArray:(NSArray<YWGetNewsDataStatus *> *)statusListArray{
    
    _statusListArray = statusListArray;
    
    [self.listCollectionView reloadData];
    
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.statusListArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
       YWHeaderVideoListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:YWVedioListID forIndexPath:indexPath];
           
       YWGetNewsDataStatus *status = self.statusListArray[indexPath.item];
       
       cell.status = status;
    
       return cell;
}

//定义每个UICollectionViewCell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(YWVedioListWidth, YWVedioListeHeight);
}

//定义每个UICollectionView的内边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(0, 10, 0, 10);
}

//设置垂直间距,默认的垂直和水平间距都是0
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return YWVedioListInterval;
}

//设置水平间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    self.selectedListStatus.isSelect = NO;
    YWGetNewsDataStatus *status = self.statusListArray[indexPath.item];
    status.isSelect = YES;
    self.selectedListStatus = status;
    
    [self.listCollectionView reloadData];
    
    //这里就是选播保存模型、改变UI了
    self.status = status;
    
    //把模型传回外面给外面VC保存
    if ([self.delegate respondsToSelector:@selector(hengVideoHeaderViewDidTap:withStatus:)]) {
        [self.delegate hengVideoHeaderViewDidTap:self withStatus:self.status];
    }
}

@end
